package unity.messenger;

import java.io.File;
import java.io.FileOutputStream;
import java.io.PrintStream;
import java.math.BigInteger;
import java.nio.charset.Charset;

import unity.messenger.security.Cipher;
import unity.messenger.security.KeyPair;
import unity.messenger.security.Cipher.Transformation;
import unity.messenger.security.KeyPair.Algorithm;
import unity.messenger.security.X509Certificate;
import unity.messenger.tools.Base64Codec;

public class UnityMessenger 
{
	public static void main(String[] args) throws Exception 
	{
		KeyPair keypair = new KeyPair(Algorithm.ECDSA_512);
		X509Certificate cert_ec = new X509Certificate("CN=wtv", keypair, new BigInteger("123456789"), null, null);
		
//		X509Certificate cert = new X509Certificate(new File("C:\\Users\\João Dias Amaro\\Desktop\\ist.cer"));
//		
//		System.out.println(cert_ec.getSignatureAlgorithm());
//		
//		System.out.println("PK1: " + new Base64Codec().encodeBytes(keypair.getPublicKey().getEncoded()));
//		System.out.println("PK1: " + new Base64Codec().encodeBytes(cert_ec.getPublicKey().getEncoded()));
//		
//		PrintStream output = new PrintStream(new File("C:\\Users\\João Dias Amaro\\Desktop\\ec.cer"));
//		output.write(cert_ec.getEncoded());
//		output.flush();
//		output.close();
		
		Cipher cipher = new Cipher(Transformation.ECIES_ECB_PKCS1Padding);
		String data = "Olá Zé!";
		byte[] encryptedData = cipher.encrypt(data.getBytes(Charset.forName("UTF-8")), keypair.getPublicKey());
		
		System.out.println(data);
		System.out.println(new Base64Codec().encodeBytes(encryptedData));
		System.out.println(new String(cipher.decrypt(encryptedData, keypair.getPrivateKey()), Charset.forName("UTF-8")));
	}
}
